# -*- coding: utf-8 -*-
# @Time    : 19-4-5 上午9:15
# @Author  : huzheng
import time
import threading
from info import pid
from data import monitor

flag = True

factor_data = [[], ]


def start(condition_dict):
    global factor_data
    process_pid = pid.get_pid(condition_dict['package_name'])
    print('测试进程pid为', process_pid)

    test_time = condition_dict['test_time']
    print('测试时间为 (单位/s)', test_time)

    th_flag = threading.Thread(target=time_flag, args=(test_time,))
    th_flag.start()
    factor_data, factor_list = select_factor(process_pid, condition_dict)

    while flag:
        temp_list = []
        for factor in factor_list:
            data = factor
            temp_list.append(data)
            time.sleep(0.3)
        factor_data.append(temp_list)

    for data in factor_data:
        print(data)
    print("测试结束")


def exchange():
    if len(factor_data):
        return factor_data


# 定时任务
def time_flag(test_time):
    global flag
    time.sleep(test_time)
    flag = False


def select_factor(process_pid, condition_dict):
    factor_title = list()
    factor_list = list()
    if condition_dict['is_cpu_test']:
        factor_title.append('cpu')
        factor_list.append(monitor.get_data('cpu', process_pid))

    if condition_dict['is_mem_test']:
        factor_title.append('memory')
        factor_list.append(monitor.get_data('memory', process_pid))

    if condition_dict['is_flow_test']:
        factor_title.append('flow')
        factor_list.append(monitor.get_data('flow', process_pid))

    if condition_dict['is_temper_test']:
        factor_title.append('temperature')
        factor_list.append(monitor.get_data('temperature', process_pid))

    if condition_dict['is_power_test']:
        factor_title.append('battery')
        factor_list.append(monitor.get_data('battery', process_pid))
    # 　一维列表变换成二维列表
    factor_data = [factor_title, ]
    return factor_data, factor_list
